package com.wanshi.lvyouwang.controller;

import com.wanshi.lvyouwang.mapper.CategoryMapper;
import com.wanshi.lvyouwang.utils.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;

/**
 * @author shkstart
 * @create 2021-11-12 16:04
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryMapper categoryMapper;

    @RequestMapping("/list")
    public String list(Model model){
        List<Category> categorys = categoryMapper.findAll();
        model.addAttribute("categorys",categorys);
        for (Category category:categorys) {
            System.err.println(category);
        }
        return "/WEB-INF/admin/category/list.html";
    }

    @RequestMapping("/add")
    public String add(){
        return "/WEB-INF/admin/category/add.html";
    }

    @RequestMapping("/addHandle")
    public String addHandle(Category category){
        categoryMapper.save(category);
        return "redirect:/category/list";
    }

    @RequestMapping("/edit")
    public String edit(Integer id,Model model){
        Category category = categoryMapper.getById(id);
        System.err.println(category);
        model.addAttribute("category",category);

        return "/WEB-INF/admin/category/edit.html";
    }

    @RequestMapping("/update")
    public String update(Category category){
        categoryMapper.save(category);
        return "redirect:/category/list";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        categoryMapper.deleteById(id);
        return "redirect:/category/list";
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "哈喽，我提交到gitee了";
    }





}
